package demo.dcs.util;

import demo.java.lang.management.vo.Annotation;
import demo.java.lang.management.vo.BinaryAnnotation;
import demo.java.lang.management.vo.ClientConstants;
import demo.java.lang.management.vo.XdcsConstants;
import demo.java.lang.management.vo.XmlySpan;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author buffoon
 * @date 15/12/15
 */
public class XmlySpanUtil {


    public static int getDuration(XmlySpan xmlySpan) {
        List<Annotation> annotations = xmlySpan.getAnnotations();

        Long duration = null;
        for (Annotation annotation : annotations) {
            if (Annotation.CLIENT_RECEIVE.equals(annotation.getValue()) || Annotation.SERVER_SEND.equals(annotation.getValue())) {
                duration = annotation.getDuration();
                break;
            }
        }

        return duration == null ? 0 : duration.intValue();
    }

    public static int getClientDuration(XmlySpan xmlySpan){
        int durationTime=0;
        for (Annotation annotation :xmlySpan.getAnnotations()) {
            if (StringUtils.equals(annotation.getValue(), "cr")) {
                durationTime = annotation.getDuration().intValue();
                break;
            }
        }
        return durationTime;
    }

    public static int getDurationFromProps(XmlySpan xmlySpan){
        int costtime = 0;
        Map<String, String> props = xmlySpan.getProps();
        if (props!=null && props.size()>0){
            costtime = Integer.parseInt(props.get(ClientConstants.X_B3_SERVER_COSTTIME));
        }
        return costtime;
    }

    public static String getSqlResult(XmlySpan dbXmlySpan){
        String sqlResult = getBinaryAnnotationValue(dbXmlySpan, XdcsConstants.DB_EXECUTE_RESULT_ANNOTATION_KEY);
        if (StringUtils.equalsIgnoreCase(sqlResult, "true")) {
            return XdcsConstants.TAG_RESULT_CODE_VALUE_SUCCESS;
        }else {
            return XdcsConstants.TAG_RESULT_CODE_VALUE_ERROR;
        }
    }

    public static String getDBName(XmlySpan dbXmlySpan){
        String dbName=null;
        String dataBaseURL = getBinaryAnnotationValue(dbXmlySpan, XdcsConstants.DB_JDBC_URL_ANNOTATION_KEY);
        String regex = "((.*/)(.*))";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dataBaseURL);
        if (matcher.matches()) {
            dbName = matcher.group(3);
        }
        return StringUtils.trim(dbName);
    }

    public static String getJDBCUrl(XmlySpan dbXmlySpan){
        return StringUtils.trim(getBinaryAnnotationValue(dbXmlySpan, XdcsConstants.DB_JDBC_URL_ANNOTATION_KEY));
    }


    public static String getSQLString(XmlySpan dbXmlySpan){
        return StringUtils.trim(getBinaryAnnotationValue(dbXmlySpan, XdcsConstants.DB_EXECUTE_SQL_ANNOTATION_KEY));
    }


    private static String getBinaryAnnotationValue(XmlySpan xmlySpan, String key){
        String annoValue=null;
        for (BinaryAnnotation binaryAnnotation : xmlySpan.getBinaryAnnotations()) {
            if (StringUtils.equalsIgnoreCase(key, binaryAnnotation.getKey())) {
                annoValue = binaryAnnotation.getValue();
                break;
            }
        }

        return annoValue;
    }

    public static void main(String[] args) {
        String json = "{\"annotations\":[{\"timestamp\":1450149591098,\"value\":\"cs\"},{\"duration\":5,\"timestamp\":1450149591103,\"value\":\"cr\"}],\"appInfo\":{},\"appName\":\"testweb\",\"binaryAnnotations\":[{\"key\":\"db.jdbc.url\",\"type\":\"sql\",\"value\":\"jdbc:mysql://192.168.1.161:3306/xdcs_home\"},{\"key\":\"db.execute.sql\",\"type\":\"sql\",\"value\":\" update test_xdcs\\n        set password='test124'\\n        WHERE id = 112\"},{\"key\":\"db.execute.result\",\"type\":\"sql\",\"value\":\"true\"}],\"deviceInfo\":{\"resolution\":{}},\"ip\":\"192.168.17.54\",\"isRowData\":false,\"isSample\":true,\"location\":{},\"monitorId\":\"8120339712720336155\",\"monitorPId\":\"5524232128242209582\",\"props\":{\"x-span-source\":\"x-java\"},\"serviceId\":\"/DBEnd\",\"spanId\":\"7622039125226098046\",\"spanType\":\"SQL\",\"traceId\":\"7578349647481999191\",\"ts\":1450149591098}";
        XmlySpan xmlySpan = JsonUtils.fromJson(json, XmlySpan.class);
        System.out.println(getClientDuration(xmlySpan));
        System.out.println(getSqlResult(xmlySpan));
        System.out.println(getDBName(xmlySpan));
        System.out.println(getSQLString(xmlySpan));
    }
}
